<?php
//enabling cookies for this page
session_start();
?>

<html>
<head>
<meta http-equiv="refresh" content="10"> 
<style type="text/css" media="only screen and (max-device-width: 480px)">
table {width:100%; font-family:Arial, Helvetica, sans-serif;}
a {font-family:Arial, Helvetica, sans-serif; font-size:80px; font-weight:bold;}
.font {font-size:55px;}
.font1 {font-size:75px;}
td {width:70%;}
input {width:90; height:100}<br>
</style>
</head>
<body>



<?php
/*
* teamStatus.php gets information from the db's emergency_team_assembly_status table and displays it
* to the user while also allowing the statuses of those entries to be changed. It also checks automatically
* for member resposnes taking more than three minutes via constant refreshes and a time value kept in the db
* and automatically makes the team_member as unavailable, and emails the next team member.
*/

/************************
* User variables to set.
* $inHouse - the background color, in hexidecimal, that will display for a field marked as 'in house'.
* $onRoute - the background color that will display for a field marked as 'on route'
* $unavailable- the background color that will display for a field marked as 'unavailable'
*
* $body - the message displayed in the summons email
* $from - the address that will appear in the 'from' field of the summons email
* $subect - The subject line of the summons email
************************/
$inHouse = "#00FF66";
$onRoute ="#1589FF";
$unavailable = "#FFFFcc";

$from = 'From: Admin@ECFweb.org';
$subject = 'Emergency Cesarean Delivery Required - Response Needed';
$body = 'Emergency Cesarean Delivery Required, use the following link 3 within minutes to report your status: http://flightdeck.ie.orst.edu/ECD/index.php?page=team_status.php';

$_SESSION['dbhost'] = 'engr-db.engr.oregonstate.edu:3307';
$_SESSION['dbname'] = 'funkecd';
$_SESSION['dbuser'] = 'funkecd';
$_SESSION['dbpass'] = 'ktrDtj1b';

$mysql_handle = mysql_connect($_SESSION['dbhost'], $_SESSION['dbuser'], $_SESSION['dbpass'])
    or die("Error connecting to database server");

mysql_select_db($_SESSION['dbname'], $mysql_handle)
    or die("Error selecting database: $dbname");

//gathering and setting some display and functional information
$equery = "SELECT `emid` FROM `Emergency` ORDER BY `emid` DESC;";
$result = mysql_query($equery);
$row = mysql_fetch_array($result);
$emid = $row['emid'];
$regEmail = '^[a-zA-Z0-9._%-]{1,50}@[a-zA-Z0-9.-]{1,50}\.[a-zA-Z]{2,4}$';
$roles = array();
$query = "SELECT `role` FROM `Team_Roles`";
$result = mysql_query($query);
$i = 1;
while($row = mysql_fetch_array($result))
{
	$roles[$i] = $row['role'];
	$role = $roles[$i];
	$_SESSION["$role"] = 1;
	$i++;
}

//setting up some internal variables
$time = time();
$team = array();
$flag = 0;
$rArr = array();
$dArr = array();
$emailL = array();
$delay = 60;
$e = 0;
$i = 0;
$j = 1;

//setting up the HTML form
echo '<form method="post" action="index.php?page=team_status.php">';
echo '<font face= Arial, Helvetica, sans-serif size = "6">';
echo 'Team Status<br>';

/*
for loop that is looking to see if a team member is going to become unavailable. 
if so, we set it to render the next member on the call list or display
the team role as being unavailable
*/
for($i = 1; $i <= 8; $i++)
{
	$j = $i * 3;
	$k = $j + 1;
	$l = $k + 1;
	$done = 0;
	$swap = 0;
	$but = 0;

	//this block is trying to see if something ahead of the "current" doctor is done, ie if we need to display buttons or not
	$query = "SELECT * FROM `Emergency_Team_Assembly_Status` WHERE (status = 2 or status = 3) and trid = $i and emid = $emid";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$rpri = $row['rpri'];
	if($rpri != null)
	{
		$done = 1;
		$dArr[$i] = $done;
	}
	else if($done == 0)
	{
		$query = "SELECT * FROM `Emergency_Team_Assembly_Status` WHERE status = 4 and trid = $i and emid = $emid";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result);
		$rpri = $row['rpri'];

		if($rpri == null)
		{
			$rpri = 4;
			$rArr[$i] = 4;
			$done = 2;
			$dArr[$i] = $done;
		}
	}

	if($_POST["$j"]) //for unavailable
	{
		$query = "SELECT * FROM `Emergency_Team_Assembly_Status` WHERE status > 1 and trid = $i and emid = $emid";

		$result = mysql_query($query);
		$row = mysql_fetch_array($result);
		$rpri = $row['rpri'];
		$t = time();
		$query = "UPDATE  `Emergency_Team_Assembly_Status` 
		SET  `status` =  '1' , `callTime` = '$t'
		WHERE  `Emergency_Team_Assembly_Status`.`trid` = $i and `Emergency_Team_Assembly_Status`.`emid` = $emid 
		and `Emergency_Team_Assembly_Status`.`status` > 1;";
		$result = mysql_query($query);
		
		/*
		checking to see if we've set less than three (not 3) team members as being unavailable. If so
		we're going to set the next team member as being 'active', and set them needing to be emailed
		if they have a valid email address.
		*/
		if($rpri !=  3) 
		{				
			$t = $t + $delay;
			$rpri++;
			$query = "UPDATE  `Emergency_Team_Assembly_Status` 
			SET  `status` =  '4' , `callTime` = '$t'
			WHERE  `Emergency_Team_Assembly_Status`.`trid` = $i and `Emergency_Team_Assembly_Status`.`emid` = $emid 
			and `Emergency_Team_Assembly_Status`.`rpri` = $rpri and `Emergency_Team_Assembly_Status`.`status` = 0;";
			$result = mysql_query($query);
			
			$query = "SELECT * FROM `Emergency_Team_Assembly_Status` WHERE status = 4 and trid = $i and emid = $emid";
			$result = mysql_query($query);
			$row = mysql_fetch_array($result);
			$tmid = $row['tmid'];
			
			$altQ = "SELECT * FROM `Team_Member_List` WHERE tmid = '$tmid' ";
			$result = mysql_query($altQ);
			$row = mysql_fetch_array($result);
			$temp = $row['member_email'];
			
			if(ereg($regEmail, $temp))
			{
				$emailL[$e++] = "$temp";	
			}
			
		}
		else //we've set three team members unavailable and need to mark the role as unavailable
		{
			$rpri = 4;
			$rArr[$i] = $rpri;
			$done = 2;
			$dArr[$i] = $done;
		}
		//clearing the button value
		$_POST["$j"] = null;
		

	}
	else if ($_POST["$k"]) //for on the way
	{	
		$t = time();
		$query = "UPDATE  `Emergency_Team_Assembly_Status` 
		SET  `status` =  '2' , `callTime` = '$t'
		WHERE  `Emergency_Team_Assembly_Status`.`trid` = $i and `Emergency_Team_Assembly_Status`.`emid` = $emid and `Emergency_Team_Assembly_Status`.`status` > 1;";
		$result = mysql_query($query);
		if ($rpri < 3) {
		$dArr[$i] = 1; }
		else {
			$rArr[$i] = 1;
			$dArr[$i] = 1;
			$rpri++;
		}
	}
	else if ($_POST["$l"]) //for in house
	{
		$t = time();
		$query = "UPDATE  `Emergency_Team_Assembly_Status` 
		SET  `status` =  '3' , `callTime` = '$t'
		WHERE  `Emergency_Team_Assembly_Status`.`trid` = $i and `Emergency_Team_Assembly_Status`.`emid` = $emid and `Emergency_Team_Assembly_Status`.`status` > 1;";
		$result = mysql_query($query);
		if ($rpri < 3) {
		$dArr[$i] = 1; }
		else {
			$rArr[$i] = 1;
			$dArr[$i] = 1;
			$rpri++;
		}
	}

	//gathering more data to display
	
	//emid, trid, tmid, status
	$query = "SELECT * FROM `Emergency_Team_Assembly_Status` 
			WHERE emid = $emid AND trid =$i ORDER BY `rpri` ASC";
	$result = mysql_query($query);
	$m = 1;
	
	$role = $roles[$i];
	while ($row = mysql_fetch_array($result)) 
	{
		$team[$i][$m] = $row;
		$tmid = $team[$i][$m]['tmid'];
		$altQ = "SELECT * FROM `Team_Member_List` WHERE tmid =$tmid"; //
		$altRes = mysql_query($altQ);
		$altRow = mysql_fetch_array($altRes);
		
		$r = $team[$i][$m]['rpri'];
		$status = $team[$i][$m]['status'];
		$team[$i][$m]['member_name'] = $altRow['member_name'];
		$team[$i][$m]['email'] = $altRow['member_email'];
		$name = $team[$i][$m++]['member_name'];
	}
	
	//gathering status of all team members
	$stat1 = $team[$i][1]['status'];
	$stat2 = $team[$i][2]['status'];
	$stat3 = $team[$i][3]['status'];	
	
	//gathering all the call + end times for the team members
	$time1 = $team[$i][1]['callTime'];
	$time2 = $team[$i][2]['callTime'];
	$time3 = $team[$i][3]['callTime'];
	$time4 = $time3 + $delay;
	
	//logic block to see if current call member is over time and need to be marked as unavailable
	if(($time > $time2 && $stat1 == 4) || (($time > $time3 && $stat2 == 4)) || (($time > $time4 && $stat3 == 4)))
	{
		$t = time();
		$query = "UPDATE  `Emergency_Team_Assembly_Status` 
		SET  `status` =  '1', `callTime` = '$t'
		WHERE  `Emergency_Team_Assembly_Status`.`trid` = $i and `Emergency_Team_Assembly_Status`.`emid` = $emid and `Emergency_Team_Assembly_Status`.`status` = 4;";
		$result = mysql_query($query);
		
		$t = $t + $delay;
		
		$rpri++;
		$query = "UPDATE  `Emergency_Team_Assembly_Status` 
		SET  `status` =  '4', `callTime` = '$t'
		WHERE  `Emergency_Team_Assembly_Status`.`trid` = $i and `Emergency_Team_Assembly_Status`.`emid` = $emid and `Emergency_Team_Assembly_Status`.`rpri` = $rpri and `Emergency_Team_Assembly_Status`.`status` = 0;";
		$result = mysql_query($query);
		
		$query = "SELECT * FROM `Emergency_Team_Assembly_Status` WHERE status = 4 and trid = $i and emid = $emid";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result);
		$tmid2 = $row['tmid'];
		$t2 = $row['callTime'];
			
		$altQ = "SELECT * FROM `Team_Member_List` WHERE tmid =$tmid";
		$result = mysql_query($altQ);
		$row = mysql_fetch_array($result);
			
		if(ereg($regEmail, $row['member_email']))
		{
			$emailL[$e++] = $row['member_email'];	
		}
	}
	//setting some internal data for later decitions
	$role = $roles[$i];
	$status = $team[$i][1]['status'];
}
//for loop that is displaying the status of team members active/unavailable and setting up buttons
for($i = 1; $i <= 8; $i++)
{
	$j = $i * 3;
	$k = $j + 1;
	$l = $k + 1;
	//this is going to be the heart of the print loop
	echo '<table border=1 style="border-collapse: collapse" width="60%">';
	for($n = 1; $n <= 3; $n++)
	{	
		$status = $team[$i][$n]['status'];
		$good = "goodup.png";
		$bad = "badup.png";
		$mid = "midup.png";
		$role = $roles[$i];
		$avail = "";
		$b = "";
	
		//setting the table rows
		if($status != 0)
		{
			if($status == 3) //in house
			{
				$bg = $inHouse;
				$good = "gooddown.png";
				$done = 1;
				$dArr[$i] = $done;
				$avail = "";
			}
			else if ($status == 2) //on route
			{
				$bg=$onRoute;
				$mid = "middown.png";
				$done = 1;
				$dArr[$i] = $done;
				$avail = "";
			}	
			else if ($status == 1) //unavailable
			{
				//unavailable
				$bad = "baddown.png";
				$bg = $unavailable;
				$avail = " - unavailable";
				$b = "<font class=\"font\" >";
			}
			else
			{
				$bg="white";
				$avail = "";
			}
			
			//determining if we need buttons
			if($status != 0 && $status != 1)
			{
				$buttons = "
					<input type =\"image\" src=\"$bad\" NAME =\"$j\" Value =\"1\" width=\"24\" height = \"24\">
					<input type =\"image\" src=\"$mid\" NAME =\"$k\" Value =\"1\" width=\"24\" height = \"24\">
					<input type =\"image\" src=\"$good\" NAME =\"$l\" Value =\"1\" width=\"24\" height = \"24\">";
			}
			else
			{
				$buttons = "";
			}
			
			//setting the table row we want to display
			$name = $team[$i][$n]['member_name'];
			$btag = "<td width = \"70%\" bgcolor=\"$bg\" align = \"top\">";
			$td   = "<td  width = \"70%\" bgcolor=\"$bg\" align = \"left\"><font class=\"font\"size=\"3\"><b>";
			$td1  = "<td  width = \"70%\" bgcolor=\"$bg\" align = \"left\"><font class=\"font1\"size=\"5\"><b>";
			$td2  = "<td  width = \"70%\" bgcolor=\"white\" align = \"left\"><font class=\"font1\"size=\"5\"><b>";
			
			if($n == 1 && $dArr[$i] != 2) //first row of names has buttons
			{
				$message = "<tr> $td2 $role <td bgcolor = \"white\" ></tr><tr>$td$b $name$avail $btag $buttons";
			}			
			else if ($n == 1 && $dArr[$i] == 2) //first row of names does not have buttons
			{
				$message = "<tr> $td2 $role </tr><tr>$td$b $name$avail </tr></td>";
			}
			else if ($n > 1 && $dArr[$i] == 2) //greater than the first row with no buttons
			{
				$message = "<tr>$td$b $name$avail ";
			}			
			else //greater than the first row with buttons
			{
				$message = "<tr>$td$b $name$avail $btag $buttons";
			}
			//displaying the table row
			echo "$message";
		}
	}
	
	//seeing if we need to make the role as unavailable, and doing so if required
	if($rArr[$i] == 4)
	{
		$bg = "#FF0055";
		$buttons = "";
		$role = "<font class=\"font\" >$role - unavailable";
		$btag = "<td bgcolor=\"$bg\" align = \"top\">";
		$td = "<td bgcolor=\"$bg\" align = \"left\"><font size=\"3\">";		
		$message = "<tr>$td $role </tr></td>";
		echo "$message";
	}
	//ending the current roles table
	echo "</table><br>";
}

//ending the html form
echo "</form></body>";

//sending any required emails
for($i = 0; $i < $e; $i++)
{
	$to = $emailL[$i];
	$sent = mail($to, $subject, $body, $from);
}

mysql_close($mysql_handle);
?>
